package com.woniuxy.creditsystem.collectionmag.controller;


import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.collectionmag.service.ICollectionService;
import com.woniuxy.creditsystem.common.bean.*;
import com.woniuxy.creditsystem.common.bean.ResultMsg;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.ArrayList;

@RequestMapping("/collection")
@RestController
public class CollectionController {

    @Resource
    private ICollectionService collectionServiceImpl;

        @Resource
        private JavaMailSender mailSender;



    @RequestMapping(value="/page",produces = {"application/json;charset=utf-8"})
    public TableResult<CollectionInfo> findAllByParams(Integer pageNum, Integer pageSize,CollectionFind collectionFind){

        System.out.println(pageNum);
        System.out.println(pageSize);
        PageInfo<CollectionInfo> pageInfo = collectionServiceImpl.findAllByParams( pageNum,pageSize,collectionFind);
        System.out.println(pageInfo);

        return new TableResult<CollectionInfo>(pageInfo.getTotal(),pageInfo.getList());
    }


    @PutMapping(value="/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg updateVehicle(CollectionInfo collectionInfo, @PathVariable("id") Long id){
        System.out.println(id);
        System.out.println(collectionInfo);

        ResultMsg resultMsg = new ResultMsg(200,"新增成功",null);
        collectionServiceImpl.updateCollectionInfo(collectionInfo);
        return resultMsg;
    }

    @GetMapping(value="/cEmail",produces = {"application/json;charset=utf-8"})
    public ResultMsg send(String email ) throws MessagingException {
        System.out.println("send");
        System.out.println(email);
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("1053421705@qq.com");
        helper.setTo(email);
        helper.setSubject("主题：发送有附件的邮件");
        helper.setText("你该还钱了。");
        File f = new File("d:/image/cc.jpg");
         //获取附件
         helper.addAttachment("file1",f);
        System.out.println("发送");
         mailSender.send(mimeMessage);

        ResultMsg resultMsg = new ResultMsg(200,"发送成功",null);
        return resultMsg;
    }

    @RequestMapping(value="/id",produces = {"application/json;charset=utf-8"})
    public ResultMsg findRPIById(CustomerInfo customerInfo){

        System.out.println(customerInfo.getId());
        CustomerInfo customerInfo1 = collectionServiceImpl.findRPIById(customerInfo.getId());
        System.out.println("查询RPI");
        ResultMsg resultMsg = new ResultMsg(200,"查询成功",customerInfo1);
        System.out.println("\ncustomerInfo1="+customerInfo1);

        return resultMsg;
    }

    /**
     * 　　*　判断此邮件是否需要回执，如果需要回执返回"true",否则返回"false" 　
     */
    private MimeMessage mimeMessage = null;
    public boolean getReplySign() throws MessagingException
    {

        boolean replySign = false;

        String needReply[] = mimeMessage.getHeader("Disposition-Notification-To");

        if (needReply != null)
        {
            replySign = true;
        }
        if (replySign)
        {
            System.out.println("该邮件需要回复");
        }
        else
        {
            System.out.println("该邮件不需要回复");
        }
        return replySign;
    }
}
